/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.ui.beans;

import com.pos.domain.User;
import com.pos.services.UserService;
import com.pos.ui.jsf.util.JsfUtil;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author suraj
 */
@Named("userBean")
@RequestScoped
public class UserBean implements Serializable {

    @Inject
    private transient UserService userService;
    private String confirmPassword;
    private User user;
    private boolean editMode;

    @PostConstruct
    public void reset() {
        this.user = new User();
    }

    public User getUser() {
        return user;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public boolean isEditMode() {
        return this.editMode;
    }

    public void createNewUser() {
        this.user = new User();
        setEditMode(true);
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public void save() {
        if (this.confirmPassword == null || !this.confirmPassword.equals(user.getPassword())) {
            JsfUtil.addErrorMessage("Password and confirm password doesn't match");
        }

        this.userService.addEmployee(user);
        this.reset();
        setEditMode(false);
    }

    public void edit(Object id) {
        this.user = this.userService.findByLodinId((String) id);
        this.confirmPassword = this.user.getPassword();
        setEditMode(true);
    }

    public List<User> getUsers() {
        return this.userService.findAll();
    }
}
